import sys
import json
import os


def env_path_set(env):
    if env == 'online':
        sys_path = '/data/whale_ops/auto_test/qa_ci_cd'
    elif env == 'linshen':
        sys_path = 'E://wt//new//qa_ci_cd'
    elif env == 'wenzixin':
        sys_path = '/Users/wendada/PycharmProjects/qa_ci_cd'
    else:
        sys_path = os.path.dirname(os.path.dirname(os.getcwd()))

    sys.path.append(sys_path)
    with open(sys_path + '/wt/init_param/read_param/url_init', 'r') as file:
        data = json.load(file)

    data['sys_path'] = sys_path
    with open(sys_path + '/wt/init_param/read_param/url_init', 'w') as file:
        json.dump(data, file, indent=4)


env_path_set("wenzixin")

from flask import Flask, request
from wt_model import WTModel

app = Flask(__name__)
wt_model = WTModel()


@app.route('/')
def hello_world():
    return 'Hello, wt自动化调试页面，准备好冲 guys!'


@app.route('/db_create', methods=['POST'])
def db_create():
    """
    输入的URL：http://127.0.0.1:5001/db_create
    :return:
    """
    req = request.get_json()
    db_type = req.get("db_type")
    table = req.get("table")
    text_type = req.get("text_type", None)
    print("传入的参数值是：db_type {}, table {}, text_type {}".format(db_type, table, text_type))
    res = wt_model.db_create(db_type, table, text_type)
    return res


@app.route('/exec_ssh_command', methods=['POST'])
def exec_ssh_command():
    req = request.get_json()
    ip = req.get("ip")
    user = req.get("user")
    passwd = req.get("passwd")
    command = req.get("command")
    print("传入的参数值是：ip {}, user {}, passws {}, command {}".format(ip, user, passwd, command))
    res = wt_model.exec_ssh_command(ip, user, passwd, command)
    return res


@app.route('/db_create_data', methods=['POST'])
def db_create_data():
    """
    输入的URL：http://127.0.0.1:5001/db_create_data
    :return:
    """
    req = request.get_json()
    db_type = req.get("db_type")
    table = req.get("table")
    num = req.get("num")
    print("传入的参数值是：db_type {}, table {}, num {}".format(db_type, table, num))
    res = wt_model.db_create_data(db_type, table, num)
    return res


@app.route('/db_oracle_make', methods=['POST'])
def db_oracle_make():
    req = request.get_json()
    db = req.get("db")
    table = req.get("table")
    num = req.get("num")
    print("传入的参数值是：db {}, table {}, num {}".format(db, table, num))
    res = wt_model.db_oracle_make(db, table, num)
    return res


@app.route('/exec_customer_sql', methods=['POST'])
def exec_customer_sql():
    """
    输入的URL：http://127.0.0.1:5001/exec_customer_sql
    :return:
    """
    req = request.get_json()
    db_type = req.get("db_type")
    table = req.get("table")
    sql = req.get("sql", None)
    print("传入的参数值是：db_type {}, table {}, sql {}".format(db_type, table, sql))
    res = wt_model.exec_customer_sql(db_type, table, sql)
    return res


@app.route('/query_alert_detail', methods=['POST'])
def query_alert_detail():
    """
    输入的URL：http://127.0.0.1:5001/query_alert_detail
    :return:
    """
    req = request.get_json()
    job_name = req.get("job_name")
    print("传入的参数值是：job_name {}".format(job_name))
    res = wt_model.query_alert_detail(job_name)
    return res


@app.route('/db_get_count', methods=['POST'])
def db_get_count():
    """
    输入的URL：http://127.0.0.1:5001/db_get_count
    :return:
    """
    req = request.get_json()
    db_type = req.get("db_type")
    table = req.get("table")
    text_type = req.get("text_type", None)
    print("传入的参数值是：db_type {}, table {}".format(db_type, table))
    res = wt_model.db_get_count(db_type, table, text_type)
    return res


@app.route('/db_delete', methods=['POST'])
def db_delete():
    """
    输入的URL：http://127.0.0.1:5001/db_delete
    :return:
    """
    req = request.get_json()
    db_type = req.get("db_type")
    table = req.get("table")
    text_type = req.get("text_type", None)
    print("传入的参数值是：db_type {}, table {}".format(db_type, table))
    res = wt_model.db_delete(db_type, table, text_type)
    return res


@app.route('/db_deleted_data', methods=['POST'])
def db_deleted_data():
    """
    输入的URL：http://127.0.0.1:5001/db_deleted_data
    :return:
    """
    req = request.get_json()
    db_type = req.get("db_type")
    table = req.get("table")
    print("传入的参数值是：db_type {}, table {}".format(db_type, table))
    res = wt_model.db_deleted_data(db_type, table)
    return res


@app.route('/db_update_data', methods=['POST'])
def db_update_data():
    """
    输入的URL：http://127.0.0.1:5001/db_update_data
    :return:
    """
    req = request.get_json()
    db_type = req.get("db_type")
    table = req.get("table")
    print("传入的参数值是：db_type {}, table {}".format(db_type, table))
    res = wt_model.db_update_data(db_type, table)
    return res


@app.route('/db_check_update_data', methods=['POST'])
def db_check_update_data():
    """
    输入的URL：http://127.0.0.1:5001/db_create
    :return:
    """
    req = request.get_json()
    source_db_type = req.get("source_db_type")
    source_table = req.get("source_table")
    sink_db_type = req.get("sink_db_type")
    sink_table = req.get("sink_table")
    print("传入的参数值是：source_db_type {}, source_table {}, sink_db_type {}, sink_table {}"
          .format(source_db_type, source_table, sink_db_type, sink_table))
    res = wt_model.db_check_update_data(source_db_type, source_table, sink_db_type, sink_table)
    return res


@app.route('/import_wt_task', methods=['POST'])
def import_wt_task():
    req = request.get_json()
    project_name = req["project_name"]
    job_name = req["job_name"]
    response = wt_model.import_wt_task(project_name, job_name)
    return response


@app.route('/get_wt_log', methods=['POST'])
def get_wt_log():
    req = request.get_json()
    project_name = req["project_name"]
    wt_type = req["wt_type"]
    task_name = req["task_name"]
    response = wt_model.get_wt_task_log(project_name, task_name, wt_type)
    return response


@app.route('/db_query', methods=['GET'])
def db_manager():
    """
    输入的URL：http://127.0.0.1:5001/db_manager?source=mysql.qa_source.aaa1&sink=mysql.qa_sink.aaa1
    :return:
    """
    source_type = request.args.get('source_type')
    source_table = request.args.get('source_table')
    sink_type = request.args.get('sink_type')
    sink_table = request.args.get('sink_table')
    response = wt_model.get_db_source_sink_count(source_type, source_table, sink_type, sink_table)
    return response


@app.route('/alert_set_query', methods=['post'])
def alert_set_or_query():
    req = request.get_json()
    jobid_or_jobname = req["jobid_or_jobname"]
    method = req["method"]
    res = wt_model.alert_set_or_query(jobid_or_jobname, method)
    return res


@app.route('/create_wt_job', methods=['post'])
def create_wt_job():
    """
    输入的URL：http://127.0.0.1:5001/create_wt_job
    :return:
    """
    req = request.get_json()
    project_name = req["project_name"]
    job_name = req["job_name"]
    job_mode = req["job_mode"]
    source_type = req["source_type"]
    sink_type = req["sink_type"]
    source_table_is_customer = None
    sink_table_is_customer = None
    source_config = None
    sink_config = None
    job_type = "DATA_INTEGRATION"
    if "source_table_is_customer" in req:
        source_table_is_customer = req["source_table_is_customer"]
    if "sink_table_is_customer" in req:
        sink_table_is_customer = req["sink_table_is_customer"]
    if "source_config" in req:
        source_config = req["source_config"]
    if "sink_config" in req:
        sink_config = req["sink_config"]
    if "job_type" in req:
        job_type = req["job_type"]
    print("传入的参数值是：project_name {}, job_name {}, job_mode {}, source_type {}, sink_type {}, "
          "source_table_is_customer {}, sink_table_is_customer {}, source_config {}, job_type {} sink_config {}".format(
        project_name, job_name, job_mode, source_type, sink_type
        , source_table_is_customer, sink_table_is_customer, source_config, job_type, sink_config))

    res = wt_model.create_wt_job(project_name, job_name, job_mode, source_type, sink_type, source_table_is_customer,
                                 sink_table_is_customer, source_config, job_type, sink_config)
    return res


@app.route('/create_ws_workflow', methods=['post'])
def create_ws_workflow():
    """
    输入的URL：http://127.0.0.1:5001/create_ws_workflow
    :return:
    """
    req = request.get_json()
    project_name = req["project_name"]
    job_name = req["job_name"]
    wt_type = req["wt_type"]
    print("传入的参数值是：project_name {}, job_name {}, wt_type {}, ".format(project_name, job_name, wt_type))
    res = wt_model.create_ws_workflow(project_name, job_name, wt_type)
    return res


@app.route('/run_ws_task', methods=['post'])
def run_ws_task():
    """
    输入的URL：http://127.0.0.1:5001/run_ws_task
    :return:
    """
    req = request.get_json()
    project_name = req["project_name"]
    job_name = req["job_name"]
    wt_type = req["wt_type"]
    run_param = None
    if "run_param" in req:
        run_param = req["run_param"]
    print(
        "传入的参数值是：project_name {}, job_name {}, wt_type {}, run_param {}".format(project_name, job_name, wt_type,
                                                                                       run_param))
    res = wt_model.run_ws_task(project_name, job_name, wt_type, run_param)
    return res


@app.route('/wait_task_complete', methods=['post'])
def wait_task_complete():
    """
    输入的URL：http://127.0.0.1:5001/wait_task_complete
    :return:
    """
    req = request.get_json()
    project_name = req["project_name"]
    job_name = req["job_name"]
    wt_type = req["wt_type"]
    print("传入的参数值是：project_name {}, job_name {}, wt_type {}, ".format(project_name, job_name, wt_type))
    res = wt_model.wait_task_complete(project_name, job_name, wt_type)
    return res


@app.route('/get_task_web_state', methods=['post'])
def get_task_web_state():
    """
    输入的URL：http://127.0.0.1:5001/get_task_web_state
    :return:
    """
    req = request.get_json()
    project_name = req["project_name"]
    job_name = req["job_name"]
    wt_type = req["wt_type"]
    print("传入的参数值是：project_name {}, job_name {}, wt_type {}, ".format(project_name, job_name, wt_type))
    res = wt_model.get_task_web_state(project_name, job_name, wt_type)
    return res


@app.route('/get_task_web_write_count', methods=['post'])
def get_task_web_write_count():
    """
    输入的URL：http://127.0.0.1:5001/get_task_web_write_count
    :return:
    """
    req = request.get_json()
    project_name = req["project_name"]
    job_name = req["job_name"]
    wt_type = req["wt_type"]
    print("传入的参数值是：project_name {}, job_name {}, wt_type {}, ".format(project_name, job_name, wt_type))
    res = wt_model.get_task_web_write_count(project_name, job_name, wt_type)
    return res


@app.route('/get_task_web_read_count', methods=['post'])
def get_task_web_read_count():
    """
    输入的URL：http://127.0.0.1:5001/get_task_web_read_count
    :return:
    """
    req = request.get_json()
    project_name = req["project_name"]
    job_name = req["job_name"]
    wt_type = req["wt_type"]
    print("传入的参数值是：project_name {}, job_name {}, wt_type {}, ".format(project_name, job_name, wt_type))
    res = wt_model.get_task_web_read_count(project_name, job_name, wt_type)
    return res


@app.route('/create_database', methods=['post'])
def create_database():
    req = request.get_json()
    db_type = req["db_type"]
    input_db_name = None
    try:
        input_db_name = req["input_db_name"]
    except Exception as e:
        print(e)
    print("传入的参数值是：db_type {} , input_db_name {}".format(db_type, input_db_name))

    res = wt_model.create_database(db_type, input_db_name)
    return res


@app.route('/del_database', methods=['post'])
def del_database():
    req = request.get_json()
    db_type = req["db_type"]
    input_db_name = None
    try:
        input_db_name = req["input_db_name"]
    except Exception as e:
        print(e)
    print("传入的参数值是：db_type {} , input_db_name {}".format(db_type, input_db_name))
    res = wt_model.del_database(db_type, input_db_name)
    return res


@app.route('/connect_database', methods=['post'])
def connect_database():
    req = request.get_json()
    db_type = req["db_type"]
    input_db_name = None
    try:
        input_db_name = req["input_db_name"]
    except Exception as e:
        print(e)
    print("传入的参数值是：db_type {} , input_db_name {}".format(db_type, input_db_name))
    res = wt_model.connect_database(db_type, input_db_name)
    return res


@app.route('/query_database', methods=['post'])
def query_database():
    req = request.get_json()
    db_type = req["db_type"]
    input_db_name = None
    try:
        input_db_name = req["input_db_name"]
    except Exception as e:
        print(e)
    print("传入的参数值是：db_type {} , input_db_name {}".format(db_type, input_db_name))
    res = wt_model.query_database(db_type, input_db_name)
    return res


@app.route('/get_database_table', methods=['post'])
def get_database_table():
    req = request.get_json()
    db_type = req["db_type"]
    input_db_name = None
    try:
        input_db_name = req["input_db_name"]
    except Exception as e:
        print(e)
    print("传入的参数值是：db_type {} , input_db_name {}".format(db_type, input_db_name))
    res = wt_model.get_database_table(db_type, input_db_name)
    return res


@app.route('/delete_project_data', methods=['POST'])
def delete_project_data():
    """
    输入的URL：http://127.0.0.1:5001/delete_project_data
    :return:
    """
    req = request.get_json()
    project_name = req.get("project_name")
    job_name = req.get("job_name")  # 设置默认值为 None

    print("传入的参数值是：project_name {}, job_name {}, ".format(project_name, job_name))
    res = wt_model.delete_project_data(project_name, job_name)
    return res


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001)
